package com.dibang.app.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.widget.LinearLayout;

/**
 * Created by wxl on 2019/7/22.
 */

public class DiffuseWaveView extends LinearLayout {

    private int width;

    private int height;

    private float currentWidth;

    private Paint paint = new Paint();

    private int cX;

    private boolean isOpening = false, isCloseing = false, isOpen;

    private OnWaveViewStatusChangeListener onWaveViewStatusChangeListener ;

    public DiffuseWaveView(Context context) {
        super(context);
        initDrawPaint();
    }

    public DiffuseWaveView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initDrawPaint();
    }

    public DiffuseWaveView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initDrawPaint();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public DiffuseWaveView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initDrawPaint();
    }


    private void initDrawPaint() {
        paint.setColor(ContextCompat.getColor(getContext(), android.R.color.white));
        paint.setStyle(Paint.Style.FILL);
        paint.setAntiAlias(true);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // widthMeasureSpec 宽度的规则 包含了两部分 模式 值
        int widthMode = MeasureSpec.getMode(widthMeasureSpec); // 模式
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);// 宽度大小
        width = widthSize - getPaddingLeft() - getPaddingRight();// 去掉左右两边的padding

        int heightMode = MeasureSpec.getMode(heightMeasureSpec); // 模式
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);// 高度大小
        height = heightSize - getPaddingTop() - getPaddingBottom();// 去掉上下两边的padding
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }



    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(ContextCompat.getColor(getContext(), android.R.color.transparent));
        if (isCloseing) {
            currentWidth -= 50;
            if (currentWidth <= 0) {
                isCloseing = false;
                currentWidth = 0;
                isOpen = false;
                canvas.drawCircle(cX, getY() + height / 2, currentWidth, paint);
                if(onWaveViewStatusChangeListener != null){
                    onWaveViewStatusChangeListener.onStatusChange(isOpen);
                }
            } else {
                setBackgroundColor(ContextCompat.getColor(getContext(), android.R.color.transparent));
                canvas.drawCircle(cX, getY() + height / 2, currentWidth, paint);
                invalidate();
            }
        } else if (isOpening) {
            currentWidth += 100;
            if (currentWidth >= width) {
                currentWidth = width;
                isOpening = false;
                isOpen = true;
                setBackgroundColor(ContextCompat.getColor(getContext(), android.R.color.white));
                canvas.drawCircle(cX, getY() + height / 2, currentWidth, paint);
                if(onWaveViewStatusChangeListener != null){
                    onWaveViewStatusChangeListener.onStatusChange(isOpen);
                }
            } else {
                canvas.drawCircle(cX, getY() + height / 2, currentWidth, paint);
                invalidate();
            }

        }
    }


    public void open() {
        if (!isOpening) {
            isOpening = true;
            isCloseing = false;
            invalidate();
        }
    }


    public void close() {
        if (!isCloseing) {
            isOpening = false;
            isCloseing = true;
            invalidate();
        }
    }

    public void setX(int cX) {
        this.cX = cX;
    }

    public interface OnWaveViewStatusChangeListener{
        void onStatusChange(boolean isOpen);
    }

    public void setOnWaveViewStatusChangeListener(OnWaveViewStatusChangeListener onWaveViewStatusChangeListener) {
        this.onWaveViewStatusChangeListener = onWaveViewStatusChangeListener;
    }
}
